package com.magicodex.bookkeeping.biz.statistic.event;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.magicodex.bookkeeping.R;
import com.magicodex.bookkeeping.biz.common.util.CheckUtil;
import com.magicodex.bookkeeping.biz.common.util.TextViewUtil;
import com.magicodex.bookkeeping.biz.statistic.constant.StatisticConstants;
import com.magicodex.bookkeeping.biz.statistic.dto.StatisticDTO;
import com.magicodex.bookkeeping.biz.statistic.util.StatisticDataUtil;
import com.magicodex.bookkeeping.ui.statistic.StatisticListViewAdapter;

import java.util.List;

/**
 * @author mh.z
 */
public class SettingFabClickListener implements View.OnClickListener {
    private Activity activity;
    private StatisticListViewAdapter adapter;

    public SettingFabClickListener(Activity activity, StatisticListViewAdapter adapter) {
        this.activity = activity;
        this.adapter = adapter;
    }

    @Override
    public void onClick(View view) {
        LayoutInflater inflater = activity.getLayoutInflater();
        View dialogView = inflater.inflate(R.layout.dialog_statistic_setting, null);

        AlertDialog.Builder builder = new AlertDialog.Builder(activity);
        builder.setTitle(R.string.dialog_title_statistic_setting);
        builder.setView(dialogView);

        builder.setPositiveButton(R.string.button_save, createPositiveButtonClickListener(dialogView));
        builder.setNegativeButton(R.string.button_cancel, createNegativeButtonClickListener());

        AlertDialog dialog = builder.create();
        SharedPreferences sharedPreferences = activity.getPreferences(Context.MODE_PRIVATE);

        // 设置账户类别前置符号
        TextView accountCategoryStartSymbolView = dialogView.findViewById(R.id.text_account_category_start_symbol);
        String accountCategoryStartSymbol = sharedPreferences.getString(StatisticConstants.PREFERENCE_ACCOUNT_CATEGORY_START_SYMBOL,
                StatisticConstants.ACCOUNT_CATEGORY_START_SYMBOL_DEFAULT);
        TextViewUtil.setString(accountCategoryStartSymbolView, accountCategoryStartSymbol);

        // 设置账户类别后置符号
        TextView accountCategoryEndSymbolView = dialogView.findViewById(R.id.text_account_category_end_symbol);
        String accountCategoryEndSymbol = sharedPreferences.getString(StatisticConstants.PREFERENCE_ACCOUNT_CATEGORY_END_SYMBOL,
                StatisticConstants.ACCOUNT_CATEGORY_END_SYMBOL_DEFAULT);
        TextViewUtil.setString(accountCategoryEndSymbolView, accountCategoryEndSymbol);

        // 显示对话框
        dialog.show();
    }

    /**
     * 点击确定按钮的事件监听器
     *
     * @param dialogView
     * @return
     */
    protected DialogInterface.OnClickListener createPositiveButtonClickListener(View dialogView) {
        CheckUtil.notNull(dialogView, "dialogView null");

        return (dialog, which) -> {
            SharedPreferences sharedPreferences = activity.getPreferences(Context.MODE_PRIVATE);
            SharedPreferences.Editor sharedPreferencesEditor = sharedPreferences.edit();

            // 保存账户类别前置符号
            TextView accountCategoryStartSymbolView = dialogView.findViewById(R.id.text_account_category_start_symbol);
            String accountCategoryStartSymbol = TextViewUtil.getString(accountCategoryStartSymbolView);
            sharedPreferencesEditor.putString(StatisticConstants.PREFERENCE_ACCOUNT_CATEGORY_START_SYMBOL, accountCategoryStartSymbol);

            // 账户类别后置符号
            TextView accountCategoryEndSymbolView = dialogView.findViewById(R.id.text_account_category_end_symbol);
            String accountCategoryEndSymbol = TextViewUtil.getString(accountCategoryEndSymbolView);
            sharedPreferencesEditor.putString(StatisticConstants.PREFERENCE_ACCOUNT_CATEGORY_END_SYMBOL, accountCategoryEndSymbol);

            // 保存共享偏好设置
            sharedPreferencesEditor.commit();

            List<StatisticDTO> recordList = StatisticDataUtil.loadStatisticRecords(activity, accountCategoryStartSymbol, accountCategoryEndSymbol);
            adapter.setRecords(recordList);
            // 处理数据变更
            StatisticDataUtil.notifyDataSetChanged(activity, adapter);
        };
    }

    /**
     * 点击取消按钮的事件监听器
     *
     * @return
     */
    protected DialogInterface.OnClickListener createNegativeButtonClickListener() {
        return (dialog, which) -> {
            //
        };
    }

}
